package com.flymobi.mshop.activity;

import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

import com.flymobi.mshop.R;
import com.flymobi.mshop.po.Order;
import com.flymobi.mshop.util.Constants;
import com.flymobi.mshop.util.service.OrderService;

public class FillOrder extends CommonActivity implements OnClickListener {
	public static Map<String, Object> valueMap = new HashMap<String, Object>();
	private String money;
	private Button shouhuoren;
	private Button zhifufangshi;
	private Button fapiao;
	private Button youhuiquan;
	private Button submit;
	private TextView jine;
	private TextView yunfei;
	private TextView youhui;
	private TextView txtyue;
	private Spinner use;
	private EditText remark;
	private ArrayAdapter<String> useadapter;
	private Map<String, String> map;
	private TextView spuse;
	private double price;
	private TextView ying;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		setContentView(R.layout.fillorder);
		System.out.println(valueMap.get("money").toString());
		money = valueMap.get("money").toString();
		Init();
		useadapter = new ArrayAdapter<String>(FillOrder.this,
				android.R.layout.simple_spinner_item, Constants.DD.SHUZU.yue);
		useadapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		use.setAdapter(useadapter);
		use.setVisibility(View.VISIBLE);
		jine.setText(money + "元");
		shouhuoren.setOnClickListener(this);
		fapiao.setOnClickListener(this);
		youhuiquan.setOnClickListener(this);
		zhifufangshi.setOnClickListener(this);
		submit.setOnClickListener(this);
		use.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				if (use.getSelectedItemId() != 0) {
					txtyue.setText(valueMap.get("yue") + "元");
					String sje = jine.getText().toString();
					System.out.println("这是sje" + sje);
					sje = sje.substring(0,
							jine.getText().toString().length() - 1);
					String syf = yunfei.getText().toString();
					syf = syf.substring(0,
							yunfei.getText().toString().length() - 1);
					double je = new Double(sje);
					Double yf = new Double(syf);
					// Double q = new Double(youhui.getText().toString());
					String sye = txtyue.getText().toString();
					sye = sye.substring(0,
							txtyue.getText().toString().length() - 1);
					Double ye = new Double(sye);
					price = je + yf - ye;
					System.out.println("这是je" + je);
					System.out.println("这是yf" + yf);
					System.out.println("这是ye" + ye);
					System.out.println("这是price" + price);
					valueMap.put("price", price);
					ying.setText(price + "");
				} else {
					txtyue.setText("0.00元");
					String sje = jine.getText().toString();
					System.out.println("这是sje" + sje);
					sje = sje.substring(0,
							jine.getText().toString().length() - 1);
					String syf = yunfei.getText().toString();
					syf = syf.substring(0,
							yunfei.getText().toString().length() - 1);
					double je = new Double(sje);
					Double yf = new Double(syf);
					// Double q = new Double(youhui.getText().toString());
					String sye = txtyue.getText().toString();
					sye = sye.substring(0,
							txtyue.getText().toString().length() - 1);
					Double ye = new Double(sye);
					price = je + yf - ye;
					System.out.println("这是je" + je);
					System.out.println("这是yf" + yf);
					System.out.println("这是ye" + ye);
					System.out.println("这是price" + price);
					valueMap.put("price", price);
					ying.setText(price + "");
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		Order.setName("");
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onRestart();
		if (valueMap.size() > 2) {
			yunfei.setText(valueMap.get("yunfei").toString());
			String sje = jine.getText().toString();
			sje = sje.substring(0, jine.getText().toString().length() - 1);
			double je = new Double(sje);
			Double yf = new Double(yunfei.getText().toString());
			// Double q = new Double(youhui.getText().toString());
			String sye = txtyue.getText().toString();
			sye = sye.substring(0, txtyue.getText().toString().length() - 1);
			Double ye = new Double(sye);
			price = je + yf - ye;
			System.out.println("这是price" + price);
			valueMap.put("price", price);
			ying.setText(price + "");
		} else {
			String sje = jine.getText().toString();
			sje = sje.substring(0, jine.getText().toString().length() - 1);
			String syf = yunfei.getText().toString();
			syf = syf.substring(0, yunfei.getText().toString().length() - 1);
			double je = new Double(sje);
			Double yf = new Double(syf);
			// Double q = new Double(youhui.getText().toString());
			String sye = txtyue.getText().toString();
			sye = sye.substring(0, txtyue.getText().toString().length() - 1);
			Double ye = new Double(sye);
			price = je + yf - ye;
			System.out.println("这是price" + price);
			valueMap.put("price", price);
			ying.setText(price + "");
		}
	}

	private void Init() {
		shouhuoren = (Button) findViewById(R.id.shouhuoren);
		zhifufangshi = (Button) findViewById(R.id.zhifufangshi);
		fapiao = (Button) findViewById(R.id.fapiao);
		youhuiquan = (Button) findViewById(R.id.youhuiquan);
		submit = (Button) findViewById(R.id.last);
		jine = (TextView) findViewById(R.id.jine);
		yunfei = (TextView) findViewById(R.id.yunfei);
		youhui = (TextView) findViewById(R.id.youhui);
		txtyue = (TextView) findViewById(R.id.yue);
		use = (Spinner) findViewById(R.id.use);
		remark = (EditText) findViewById(R.id.remark);
		spuse = new TextView(FillOrder.this);
		ying = (TextView) findViewById(R.id.yingfu2);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.last:
			if (valueMap.get("name") == "") {
				Toast.makeText(FillOrder.this, "请填写收货人信息", 1000).show();
			} else {
				valueMap.put("yue", txtyue.getText().toString());
				valueMap.put("remark", remark.getText().toString());
				valueMap.put("jine", ying.getText().toString());
				map.put("name", valueMap.get("name").toString());
				map.put("money", valueMap.get("yue").toString());
				map.put("shouji", Order.getShouji());
				map.put("dizhi", Order.getDizhi());
				map.put("youbian", Order.getYoubian());
				map.put("zhifu", Order.getZhifu());
				map.put("peisong", Order.getPeisong());
				map.put("type", Order.getType());
				map.put("danwei", Order.getDanwei());
				map.put("danweiname", Order.getDanweiname());
				map.put("leixing", Order.getLeixing());
				// map.put("quan", Order.getQuan());//数据库加载
				map.put("yue", Order.getYue());// 数据库加载
				map.put("remark", Order.getRemark());
				map.put("jine", Order.getJine());// 计算之后的结果 需要数据库数据
				OrderService.save(map);
				Intent intent = new Intent();
				valueMap.put("yingfu", ying.getText().toString());
				intent.setClass(FillOrder.this, ZhiFu.class);
				FillOrder.this.startActivity(intent);
			}
			break;
		case R.id.shouhuoren:
			Intent sintent = new Intent();
			sintent.setClass(FillOrder.this, ShouHuoRen.class);
			FillOrder.this.startActivity(sintent);
			break;
		case R.id.zhifufangshi:
			Intent zintent = new Intent();
			zintent.setClass(FillOrder.this, PeiSong.class);
			FillOrder.this.startActivity(zintent);
			break;
		case R.id.fapiao:
			Intent fintent = new Intent();
			fintent.setClass(FillOrder.this, FaPiao.class);
			FillOrder.this.startActivity(fintent);
			break;
		case R.id.youhuiquan:
			Intent yintent = new Intent();
			yintent.setClass(FillOrder.this, YouHui.class);
			FillOrder.this.startActivity(yintent);
			break;
		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);

	}

}
